/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package graphviewer;

import service.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import service.Storage;

/**
 *
 * @author Sabitov Artem
 * @date 23.09.2013
 */
public class GraphScale implements MouseWheelListener {
	private Graph[] graph;
	public static final int CENTRE_SCREEN_X = 504;
	public static final int CENTRE_SCREEN_Y = 364;
	double minX = 100000;
	double minY = 100000;
	double maxX = 0;
	double maxY = 0;
	boolean bEnd = false;
		
	public GraphScale(Graph[] graph) {
		this.graph = graph;
		initMouseWheelListener();
	}
	
	private void initMouseWheelListener() {
		Storage.getViewer().addMouseWheelListener(this);
		//autoScale();
	}
	
	private void centered() {	
		minX = 9000;
		minY = 9000;
		maxX = 0;
		maxY = 0;
	for(int k = 0; k < graph.length;k++) {
		for(int i = 0; i < graph[k].getSizeGraph(); i++) {
			Node node = graph[k].getNode(i);
			if(minX > node.getCoordX()) {
				minX = node.getCoordX();
			}
			if(maxX < node.getCoordX()) {
				maxX = node.getCoordX();
			}
			if(minY > node.getCoordY()) {
				minY = node.getCoordY();
			}
			if(maxY < node.getCoordY()) {
				maxY = node.getCoordY();
			}
		}
		Offset offset = new Offset();
		for(int i = 0; i < graph[k].getSizeGraph(); i++) {
			Node node = graph[k].getNode(i);
			if(minX > 20 && minY > 20) {
				offset.offsetX = -5;
				offset.offsetY = -5;
				node.setOffset(offset);
			} else if(minX < 15 && minY < 15) {
				offset.offsetX = 5;
				offset.offsetY = 5;
				node.setOffset(offset);
			}
		}
	}
	}
	private void autoScale() {
		int frameHieght = Storage.getViewer().getHeight() - 60;
		int frameHWidth = Storage.getViewer().getWidth() - 40;
		log("autoscale is started!");
		while(!bEnd) {
			centered();
//			try {
//				Thread.sleep(50);
//			} catch (InterruptedException ex) {
//				Logger.getLogger(GraphScale.class.getName()).log(Level.SEVERE, null, ex);
//			}
			if(maxX < frameHWidth && maxY < frameHieght) {
				scaleUp();
			} else if(maxX > frameHWidth && maxY > frameHieght) {
				scaleDown();
			} else if((frameHWidth - maxX) < 50 &&
					(frameHieght - maxY) < 50 &&
					minX > 20 &&
					minY > 20) {
				bEnd = true;
			}
		}
		log("autoscale is complited!");
	}
	
	@Override
	public void mouseWheelMoved(MouseWheelEvent mwe) {
		int notches = mwe.getWheelRotation();
		if(notches < 0) {
			scaleUp();
		} else {
			scaleDown();
		}
	}
	
	protected void scaleUp() {
		Offset offset = new Offset();
		offset.offsetX = 0;
		offset.offsetY = 0;
		Node node;
		for(int k = 0; k < graph.length; k++) {
			for(int i = 0; i < graph[k].getSizeGraph(); i++) {
				node = graph[k].getNode(i);
				offset.offsetX = node.getCoordX() / 200;
				offset.offsetY = node.getCoordY() / 200;
				node.setOffset(offset);
				node.performOffset();
				node.setNodeRadius(node.getNodeRadius() + 0.08);
				graph[k].update();
			}
		}
	}

	protected void scaleDown() {
		Offset offset = new Offset();
		offset.offsetX = 0;
		offset.offsetY = 0;
		Node node;
		for(int k = 0; k < graph.length; k++) {
			for(int i = 0; i < graph[k].getSizeGraph(); i++) {
				node = graph[k].getNode(i);
				offset.offsetX = node.getCoordX() / 200;
				offset.offsetY = node.getCoordY() / 200;
				offset.reverseOffset();
				node.setOffset(offset);
				node.performOffset();
				node.setNodeRadius(node.getNodeRadius() - 0.08);
				graph[k].update();
			}
		}
	}
	
	protected void log(Object obj) {
		if(Service.getServiceState()) {
			Service.log(obj);
		}
	}
}
